package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.User;
import com.starwish.starwishblog.mapper.UserMapper;
import com.starwish.starwishblog.vo.HistoryVo;
import com.starwish.starwishblog.vo.UserLoginVo;
import com.starwish.starwishblog.vo.UserOperationVo;
import com.starwish.starwishblog.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;


    public User login(String username) {
        return userMapper.selectUserByName(username);
    }

    public PageInfo<UserVo> queryByPages(Integer now, Integer size, String name) {
        PageHelper.startPage(now, size);
        List<UserVo> list = userMapper.selectUserByCons(name);
        return new PageInfo<>(list);

    }
    @Transactional
    public void addFollowUser(Integer userId, Integer authorId) {
        //查询是否关注过
        Integer i = userMapper.selectIdByUserIdAndAuthorId(userId, authorId);
        System.out.println("添加关注"+i);
        if(i==null){
            //没有关注就直接添加
            userMapper.insertFollowUser(userId,authorId);
        }else {
            //之前关注过，修改关注状态
            userMapper.modifyFollowStatusById(i);
        }
        //根据id查询粉丝数量 查询关注博主的数量
        Integer fanCount = userMapper.selectFanCountByKey(authorId);
        Integer followCount= userMapper.selectFollowCountByKey(userId);
        //根据id修改用户表中的粉丝和关注博主数量
        int count = userMapper.modifyFanCountByKey(authorId, fanCount);
        int i1 = userMapper.modifyFollowCountByKey(userId, followCount);
    }


    public PageInfo<UserVo> queryFansById(Integer now, Integer size, Integer id) {
        PageHelper.startPage(now, size);
        List<UserVo> list = userMapper.selectFansByUserId(id);
        return new PageInfo<>(list);
    }
    @Transactional
    public int cancelFollowUserByKey(Integer userId, Integer authorId) {
        //先取消关注
        Integer id = userMapper.selectIdByUserIdAndAuthorId(userId, authorId);
        int i = userMapper.modifyAuthorById(id);
        if(i>0){
            //根据id查询粉丝数量
            Integer fanCount = userMapper.selectFanCountByKey(authorId);
            Integer followCount= userMapper.selectFollowCountByKey(userId);
            //根据id修改用户表中的分时数量
            int count = userMapper.modifyFanCountByKey(authorId, fanCount);
            int i1 = userMapper.modifyFollowCountByKey(userId, followCount);
        }
        return i;
    }

    public int registerUserByKey(User user) {
        return userMapper.insert(user);
    }

    public int modifyLoginTime(Integer id) {
        int i = userMapper.updateLoginTimeByKey(id);
        return i;
    }

    public PageInfo<UserVo> queryUserAndFanCountByPages(Integer now, Integer size, String username) {
        PageHelper.startPage(now, size);
        List<UserVo> list = userMapper.selectUserAndFanCount(username);
        return new PageInfo<>(list);
    }

    public int modifyUserByKey(User user) {
        int i = userMapper.updateByPrimaryKey(user);
        return i;
    }

    public int deleteUserByKey(Integer id) {

        return userMapper.deleteUserByKey(id);
    }

    public User queryUserByKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public int updateLocked(Integer id, Integer status) {
        int i = userMapper.updateLocked(id,status);
        return i;
    }

    public PageInfo<HistoryVo> queryUserHistoryByPages(Integer now, Integer size, String username) {
        PageHelper.startPage(now, size);
        List<HistoryVo> list = userMapper.selectUserHistoryList(username);
        return new PageInfo<>(list);
    }

    public int insertHistoryById(HistoryVo historyVo) {
        int i = userMapper.insertHistory(historyVo);
        return i;
    }

    public int deleteUserHistoryByKey(Integer id) {
        int i = userMapper.deleteHistoryById(id);
        return i;
    }

    public int deleteUserHistoryByUserId(Integer id) {
        return userMapper.deleteHistoryByUserId(id);
    }

    public int recoverUserHistoryByKey(Integer userId) {

        return userMapper.recoverUserHistoryById(userId);
    }

    public int recoverAllUserHistory() {
        return userMapper.recoverAllHistory();
    }

    public int deleteAllUserHistory(){
        return userMapper.deleteAllHistory();
    }

    public int updateHistory(Integer id, Integer isDelete) {
        return userMapper.modifyHistoryById(id,isDelete);
    }

    public PageInfo<UserVo> queryFans(Integer now, Integer size, String username) {
        PageHelper.startPage(now, size);
        List<UserVo> list = userMapper.selectFansList(username);
        return new PageInfo<>(list);
    }

    public int addUserLoginLog(UserLoginVo loginVo) {
        int i = userMapper.insertUserLoginLog(loginVo);
        return i;
    }

    public int addUserOperationLog(UserOperationVo operationVo) {
        int i = userMapper.insertUserOperationLog(operationVo);
        return i;
    }

    public String queryFollowStatus(Integer userId, Integer authorId) {
        String s = userMapper.selectStatusUserIdAndAuthorId(userId, authorId);
        if(s==null){
            //不存在可以添加关注
            return "0";
        }else {
            return s;
        }
    }

    public PageInfo<UserVo> queryFollowById(Integer now, Integer size, Integer id) {
        PageHelper.startPage(now, size);
        List<UserVo> list = userMapper.selectFollowByUserId(id);

        return new PageInfo<>(list);
    }


    public PageInfo<HistoryVo> findUserHistoryByUserId(Integer now, Integer size, Integer userId) {
        PageHelper.startPage(now, size);
        List<HistoryVo> list = userMapper.selectHistoryByUserId(userId);
        return new PageInfo<>(list);
    }

    public PageInfo<User> findFansByUserId(Integer now, Integer size, Integer userId) {
        PageHelper.startPage(now, size);
        List<User> list = userMapper.selectFansByUid(userId);
        return new PageInfo<>(list);
    }

    public PageInfo<User> findFollowsByUserId(Integer now, Integer size, Integer userId) {
        PageHelper.startPage(now, size);
        List<User> list = userMapper.selectFollowByUid(userId);
        return new PageInfo<>(list);
    }
}
